package com.huawen.cms.app.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huawen.cms.app.entity.Article;
import com.huawen.cms.app.entity.Category;
import com.huawen.cms.app.service.ArticleService;
import com.huawen.cms.app.service.CategoryService;
import com.huawen.cms.app.util.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("/category")
@RestController
public class CategoryController extends BaseController {

	@Autowired
	private CategoryService categoryService;

	@RequestMapping(value = "/getById", method = RequestMethod.GET)
	public Category getById(Long id) {
		Category category = (Category) categoryService.getById(id);
		return category;
	}

	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	public Category insert(@RequestBody Category category) {
		categoryService.insert(category);
		return category;
	}

	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public Category update(@RequestBody Category category) {
		categoryService.update(category);
		return category;
	}

	@RequestMapping(value = "/getList", method = RequestMethod.POST)
	public List<Category> getList(@RequestBody Category category) {
		List<Category> list = categoryService.getList(category);
		return list;
	}

	@RequestMapping(value = "/getPagerList", method = RequestMethod.POST)
	public PageInfo<Category> getPagerList(@RequestBody Category category) {
		Pager pager = getCurrentPager();
		PageHelper.startPage(pager.getPageNum(), pager.getPageSize());
		List<Category> list = categoryService.getList(category);
		PageInfo<Category> pageInfo = new PageInfo<Category>(list);
		return pageInfo;
	}

}
